package com.urls;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlUploadDemo01 {
    public static void main(String[] args) throws Exception {
        // 获取网络资源
        // 1.下载地址
        URL url = new URL("http://m801.music.126.net/20220209175922/1b831c58ca327c5b478acb1985194c86/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/9444692958/7a81/b342/ea4f/fd3548f57b06dfe051a64269b4806878.m4a");
        // 2.连接到这个资源 HTTP  转成 Http连接
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        // 创建输入流 写入java程序中
        InputStream inputStream = urlConnection.getInputStream();
        // 创建一个输出流 写入保存文件中
        FileOutputStream fos = new FileOutputStream("wuding.m4a");
        // 循环读写
        byte[] buff = new byte[1024];
        int len;
        while ((len = inputStream.read(buff)) != -1) {
            // 写入文件
            fos.write(buff,0,len);
        }
        // 关闭资源流 -- 先开后关
        fos.close();
        inputStream.close();
        urlConnection.disconnect();// 断开连接
    }
}
